
" 基础日志函数（追加写入模式）
function! deepseek#log#write(msg, level)
    let g:deepseek_logfile = get(g:, 'deepseek_logfile', expand($TEMP . '/deepseek_debug.log'))
    let timestamp = strftime('%Y-%m-%d %H:%M:%S')
    let log_entry = printf("[%s][%s] %s", timestamp, a:level, a:msg)
    " 写入文件（追加模式）
    try
        if filewritable(g:deepseek_logfile)
            " echomsg 'message "' .. a:msg .. '" write to ' .. g:deepseek_logfile
            call writefile([log_entry], g:deepseek_logfile, 'a')
        else
            echomsg 'log file ' .. g:deepseek_logfile .. ' is not writable.'
        endif
    catch
        echohl ErrorMsg
        echom "日志写入失败：" . v:exception
        echohl None
    endtry
endfunction

" 分级日志快捷函数
function! deepseek#log#debug(...)
    let msg = call("printf", a:000)
    call deepseek#log#write(msg, 'DEBUG')
endfunction

function! deepseek#log#info(msg)
    call deepseek#log#write(a:msg, 'INFO')
endfunction

function! deepseek#log#warn(msg)
    call deepseek#log#write(a:msg, 'WARN')
endfunction

function! deepseek#log#error(msg)
    call deepseek#log#write(a:msg, 'ERROR')
endfunction

function! deepseek#log#open()
    let g:deepseek_logfile = get(g:, 'deepseek_logfile', expand($TEMP . '/deepseek_debug.log'))
    let cmd = 'edit ' .  g:deepseek_logfile
    execute cmd
endfunction
